<?php

namespace app\util;

use Yii;
use Da\QrCode\QrCode as Qr;
use yii\imagine\Image;

/**
 * Class Qrcode
 * @package app\util
 */
class Qrcode
{
    /**
     * Is exist
     *
     * @param $name
     * @return bool
     */
    public static function isExist($name)
    {
        $base = Yii::$app->getBasePath() . '/web';
        $path = $base . $name;

        return file_exists($path);
    }

    /**
     * generate qrcode
     *
     * @param string $string
     * @param string $name  mapping path
     * @param int $size
     * @param array $color
     * @return bool
     */
    public static function generate($string = 'test-content', $name = '/qrocde/demo.png', $size = 100, $color = [])
    {
        // save path
        $base = Yii::$app->getBasePath() . '/web';
        $path = $base . $name;

        $dir = dirname($path);
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
            chmod($dir, 0777);
        }

        // color set
        if (empty($color)) {
            $color = [
                'background' => [255, 255, 255],
                'foreground' => [0, 0, 0],
            ];
        }

        // create qrcode
        $qr = (new Qr($string))
            ->setSize($size)
            ->setMargin(5)
            ->setBackgroundColor($color['background'][0], $color['background'][1], $color['background'][2])
            ->setForegroundColor($color['foreground'][0], $color['foreground'][1], $color['foreground'][2]);
        header('Content-Type: ' . $qr->getContentType());
        $qr->writeFile($path);

        return true;
    }

    /**
     * set poster
     *
     * @param $qrPath
     * @param $posterPath
     * @param array $position
     */
    public static function setPoster($qrPath, $posterPath, $savePath = '', $position = [])
    {
        $base = Yii::$app->getBasePath() . '/web';
        $qrPath = $base . $qrPath;
        $posterPath = $base . $posterPath;

        // save path
        if (empty($position)) {
            $position = [1, 1];
        }

        if (!$savePath) {
            $savePath = $qrPath;
        } else {
            $savePath = $base . $savePath;
        }

        // notice: 750px x 1200
        Image::watermark(
            $posterPath,
            $qrPath,
            $position
        )->save($savePath, ['quality' => 100]);

        return $savePath;
    }

    /**
     * set text
     *
     * @param $sourcePath
     * @param $targetPath
     * @param string $text
     * @param array $position
     * @param int $size
     * @param string $color
     * @return mixed
     */
    public static function setText($sourcePath, $text = 'test...', $targetPath = '', $position = [], $size = 10, $color = '#000')
    {
        $base = Yii::$app->getBasePath() . '/web';
        $sourcePath = $base . $sourcePath;

        if (!$targetPath) {
            $targetPath = $sourcePath;
        } else {
            $targetPath = $base . $targetPath;
        }

        // set font
        $font = \Yii::$app->getBasePath() . '/web/fonts/yh.ttf';

        if (empty($position)) {
            $position = [1, 1];
        }

        Image::text(
            $sourcePath,
            $text,
            $font,
            $position,
            ['size' => $size, 'color' => $color]
        )->save($targetPath, ['quality' => 100]);

        return $targetPath;
    }
}